<?php
require 'config/dataSource.php';
if(isset($_GET['id'])) {

    $id = $_GET['id'];
    $fileID;
    $name;
    $size;
    $mime;
    $data;
    $status=false;
    $getQuery=  mysql_query("SELECT fileID,name,mime,size,data FROM file");
    if(mysql_num_rows($getQuery)>0){
        while($row = mysql_fetch_assoc($getQuery)){
            $fileID = $row['fileID'];
            
            if(sha1($fileID)==$id){
                $name = $row['name'];
                $size = $row['size'];
                $mime = $row['mime'];
                $data = $row['data'];
                $status = true;
                break;
            }
            
        }
        
    } else{
        echo "<script language='javascript'>alert('Sorry, file not found!');</script>";
    }
    if($status){
        header("Content-Type: ". $mime);
        header("Content-Length: ". $size);
        header("Content-Disposition: attachment; filename=". $name);
        ob_clean();
        flush();
        echo $data;
    } else {
        echo "<script language='javascript'>alert('Sorry, file not found!');</script>";
    }
    
     
}
?>